package com.yazi.mxz.mobile.controller.user;

import java.util.List;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.MemberNotice;
import com.yazi.mxz.common.entity.enums.MemberNoticeStatus;
import com.yazi.mxz.common.service.MemberNoticeService;

@Controller("user-memberNoticeController")
@RequestMapping("/user/memberNotice")
public class MemberNoticeController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(MemberNoticeController.class);

	@Resource
	MemberNoticeService memberNoticeService;

	@RequestMapping
	public String list(Model model) {
		log.debug("进入通知列表页面");
		List<MemberNotice> memberNoticeList;
		try {
			memberNoticeList = memberNoticeService.listByMemberId(getMemberId(), null).getList();

		} catch (Exception e) {
			log.error("进入通知列表页面异常:", e);
			return FORWARD_TIP;
		}
		model.addAttribute("memberNoticeList", memberNoticeList);
		return "user/memberNotice_list";
	}

	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long[] noticeIds) {
		log.debug("删除用户通知");
		StatusResult jsonResult = new StatusResult();
		try {
			memberNoticeService.removeByMemberId(getMemberId(), noticeIds);
			jsonResult.setOkMessage("删除成功");

		} catch (Exception e) {
			log.error("删除用户通知异常", e);
			jsonResult.setErrorMessage("删除失败,请联系管理员!");
		}
		return jsonResult;
	}

	@RequestMapping("/count/json")
	@ResponseBody
	public ObjectResult<Integer> count() {
		log.debug("获取最新通知数量");
		ObjectResult<Integer> jsonResult = new ObjectResult<Integer>();
		try {
			MemberNotice memberNotice = new MemberNotice();
			memberNotice.setMember(getMember());
			// 未查看
			memberNotice.setStatus(MemberNoticeStatus.UNREAD.getValue());

			List<MemberNotice> mnList = memberNoticeService.list(memberNotice);
			jsonResult.setResult(new Integer(mnList.size()));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取最新通知数量异常: ", e);
			jsonResult.setErrorMessage("获取最新通知数量失败");
		}
		return jsonResult;
	}
}
